﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.Commons.Enums
{
   /// <summary>
   /// 各种状态
   /// </summary>
    public enum StatusEnum : int
    {
        [Description("自动运行")]
        AutomaticRunning = 1,
        [Description("待料")]
        WaitMaterial = 2,
        [Description("堵料")]
        BlockingMaterial = 3,
        [Description("换料")]
        ChangeMaterial = 4,
        [Description("故障（故障停机）")]
        MTBF_FaultShutdown = 5,
        [Description("故障（故障运行）")]
        MTBF_FaultRunning = 6,
        [Description("保养")]
        Maintenance=7,
        [Description("调试")]
        Debug=8,
        [Description("其他")]
        Other=0,

    }
    public static class StatusEnumHelper
    {
        public static string GetDescriptionFromEnumValue(int value)
        {
            // 获取枚举类型的所有成员
            foreach (FieldInfo fi in typeof(StatusEnum).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                // 获取枚举成员的值
                var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                if (attribute != null)
                {
                    // 如果枚举成员的值匹配传入的值，返回该成员的 Description 特性字符串
                    if ((int)fi.GetValue(null) == value)
                    {
                        return attribute.Description;
                    }
                }
            }
            // 如果没有找到匹配的枚举成员，返回空字符串或其他默认值
            return string.Empty;
        }


        public static (List<StatusEnum> enumValues, List<int> enumValueInts, List<string> descriptions) GetEnumCollections()
        {
            List<StatusEnum> enumValues = new List<StatusEnum>();
            List<int> enumValueInts = new List<int>();
            List<string> descriptions = new List<string>();

            // 获取枚举类型的所有成员
            foreach (FieldInfo fi in typeof(StatusEnum).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                // 获取枚举成员的值并添加到列表中
                StatusEnum enumValue = (StatusEnum)fi.GetValue(null);
                enumValues.Add(enumValue);

                // 将枚举成员的值转换为整数并添加到列表中
                int value = (int)enumValue;
                enumValueInts.Add(value);

                // 获取枚举成员的 Description 特性
                DescriptionAttribute attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                string description = attribute != null ? attribute.Description : string.Empty;
                descriptions.Add(description);
            }

            return (enumValues, enumValueInts, descriptions);
        }


        public static StatusEnum GetStatusEnumByDescription(string description)
        {
            foreach (StatusEnum value in Enum.GetValues(typeof(StatusEnum)))
            {
                FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
                if (fieldInfo != null)
                {
                    DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (attributes.Length > 0 && attributes[0].Description == description)
                    {
                        return value;
                    }
                }
            }

            throw new ArgumentException("Description not found.", nameof(description));
        }

        public static string GetDescription(StatusEnum value)
        {
            FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
            if (fieldInfo != null)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    return attributes[0].Description;
                }
            }

            return value.ToString();
        }

        public static bool TryConvertToEnum<T>(this string value, out T result) where T : struct
        {
            return Enum.TryParse(value, true, out result) && Enum.IsDefined(typeof(T), result);
        }



    }
}
